package com.jqhee.hjq.ysb;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hjq on 2017/8/27.
 */

public class MyListAdapter extends BaseAdapter {
    private static final String TAG = "MyListAdapter";

    private Context mContext;
    private List<String> mListDatas = new ArrayList<>();
    private LayoutInflater mLayoutInfater;


    public MyListAdapter(Context context) {
        mContext = context;
        mLayoutInfater = LayoutInflater.from(context);
    }

    public void addList(List<String> list) {
        mListDatas = list;
    }

    @Override
    public int getCount() {
        return mListDatas.size();
    }

    @Override
    public Object getItem(int i) {
        return mListDatas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewHolder holder = null;
        if (view == null) {
            view = mLayoutInfater.inflate(R.layout.listview_item_layout, null);
            holder = new ViewHolder();
            holder.textView = (TextView)view.findViewById(R.id.tv_text);
            view.setTag(holder);
        }else  {
            holder = (ViewHolder)view.getTag();
        }
        holder.textView.setText(mListDatas.get(i));
        return view;
    }




    /**
     * 第三种方法 调用一次getView()方法；Google推荐的做法
     *
     * @param position 要更新的位置
     */
//    private void updateItem(int position) {
//        /**第一个可见的位置**/
//        int firstVisiblePosition = listView.getFirstVisiblePosition();
//        /**最后一个可见的位置**/
//        int lastVisiblePosition = listView.getLastVisiblePosition();
//
//        /**在看见范围内才更新，不可见的滑动后自动会调用getView方法更新**/
//        if (position >= firstVisiblePosition && position <= lastVisiblePosition) {
//            /**获取指定位置view对象**/
//            View view = listView.getChildAt(position - firstVisiblePosition);
//            commonAdapter.getView(position, view, listView);
//        }
//
//    }

    // 缓存view
    public static class ViewHolder {
        public TextView textView;
    }

}


